// Shoud be set by user - you should use [%id%] substring for specifying place for id
var ttUrl = "";

// Yom may leave this field as is (null) for default behavour
var ttResponseHandlerPtr = null;


var ttOffsetX = -20;
var ttOffsetY = -20;

var ttObj = null;
var ttEnable = false;

var ttText = new Array();


function ttLoadAndShow(id)
{
	if (typeof ttText[id] != 'undefined')
	{
		ttShow(id);
		return;
	}

	ttObj.innerHTML='<p class="textStd">Loading...';
	ttEnable = true;
	ttPositionTip();

	var url = ttUrl;
	url = url.replace("[%id%]", id);
	
	var rh = ttResponseHandlerPtr;
	if (!rh) rh = ttResponseHandlerDefault;
	
	ajaxLoadData(ajaxMakeUrlUniq(url), rh);
}

function ttShow(id)
{
	if (typeof ttText[id] != 'undefined')
	{
		ttObj.innerHTML = ttText[id];
		ttEnable = true;
		ttPositionTip();
	}
}

function ttResponseHandlerDefault(response)
{
	if (response.indexOf('|') != -1)
	{
		var update = new Array();
		update = response.split('|');
		ttText[update[0]] = update[1];
		
		ttShow(update[0]);
	}
	else
	{
		ttHideTip();
		alert(response);
	}
}


var ie=document.all;
var ns6=document.getElementById && !document.all;

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat") ? 
		document.documentElement : document.body;
}

var ttEvent = null;

function ttPositionTip()
{
	if (typeof event != 'undefined') {
		ttEvent = event;
	}

	if (ttEnable && ttEvent)
	{
		var curX=(ns6)?ttEvent.pageX : ttEvent.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?ttEvent.pageY : ttEvent.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-ttEvent.clientX-ttOffsetX : 
			window.innerWidth-e.clientX-ttOffsetX-20;
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-ttEvent.clientY-ttOffsetY : 
			window.innerHeight-e.clientY-ttOffsetY-20;
		
		var leftedge=(ttOffsetX<0)? ttOffsetX*(-1) : -1000;
	
		// if the horizontal distance isnt enough to accomodate the width of the context menu
		if (rightedge<ttObj.offsetWidth)
			//move the horizontal position of the menu to the left by it's width
			ttObj.style.left=ie ? 
				ietruebody().scrollLeft+ttEvent.clientX-ttObj.offsetWidth+"px" : 
					window.pageXOffset+e.clientX-ttObj.offsetWidth+"px";
		else if (curX<leftedge)
			ttObj.style.left="5px";
		else
			//position the horizontal position of the menu where the mouse is positioned
			ttObj.style.left=curX+ttOffsetX+"px";
		
		//same concept with the vertical position
		if (bottomedge < ttObj.offsetHeight)
			ttObj.style.top=ie ?  
				ietruebody().scrollTop+ttEvent.clientY-ttObj.offsetHeight-ttOffsetY+"px" : 
				window.pageYOffset+e.clientY-ttObj.offsetHeight-ttOffsetY+"px";
		else
			ttObj.style.top=curY+ttOffsetY+"px";

		ttObj.style.visibility="visible";
	}
}

function ttHideTip()
{
	if (ns6||ie)
	{
		ttEnable = false;
		ttObj.style.visibility = "hidden";
		ttObj.style.left = "-1000px";
		ttObj.style.backgroundColor = "";
		ttObj.style.width = "";
	}
}

function ttStartDocument()
{ 
	ttObj = document.getElementById("ttElem");
}
